﻿EasyCodeApp.factory('authInterceptorService', ['$q', '$location', 'localStorageService', '$window', function ($q, $location, localStorageService, $window) {
    var authInterceptorServiceFactory = {};
    var _request = function (config) {
        config.headers = config.headers || {};
        var authData = localStorageService.get('authorizationData');
        config.headers.IsAsync = 'Y';
        if (authData) {
            config.headers.Authorization = 'Bearer ' + authData.token;
        }
        return config;
    }
    var _responseError = function (rejection) {
        if (rejection.status === 401) {
            $location.path('/login');
            //$window.location.href = '/Account/Login';
        }
        return $q.reject(rejection);
    }
    authInterceptorServiceFactory.request = _request;
    authInterceptorServiceFactory.responseError = _responseError;
    return authInterceptorServiceFactory;
}]);

EasyCodeApp.config(['$httpProvider', function ($httpProvider) {
    $httpProvider.interceptors.push('authInterceptorService');
}]);